package com.china08.yxyapi.api.manage.sch.platform.live.lesson;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.live.YxyLiveLesson;
import com.china08.yxyapi.entity.db.live.YxyLiveLessonItem;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.live.YxyLiveLessonItemRepository;
import com.china08.yxyapi.repository.db.live.YxyLiveLessonRepository;

@CrossOrigin
@RestController
public class LiveSchLessonUserValidateController extends YxyApiBaseController {

    @Autowired
    private YxyLiveLessonRepository yxyLiveLessonRepository;

    @Autowired
    private YxyLiveLessonItemRepository yxyLiveLessonItemRepository;

    @RequestMapping(value = "/api_yw/manage/sch/platform/live/lesson/user/validate", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel) {

        List<YxyLiveLesson> yxyLiveLesson = yxyLiveLessonRepository
                .findByTeacherIdAndStatusNotAndAuditStatusNot(reqModel.userId, 2, 2);

        List<String> lessonIdList = new ArrayList<String>();

        List<RespModel> respModels = new ArrayList<RespModel>();
        if (yxyLiveLesson != null && yxyLiveLesson.size() > 0) {
            for (YxyLiveLesson yxyLiveLesson2 : yxyLiveLesson) {
                lessonIdList.add(yxyLiveLesson2.getId());
            }

            List<YxyLiveLessonItem> items = yxyLiveLessonItemRepository.findByLessonIdIn(lessonIdList);
            for (YxyLiveLessonItem yxyLiveLessonItem : items) {

                RespModel model = new RespModel();

                model.bTime = yxyLiveLessonItem.getBeginDate();
                model.eTime = yxyLiveLessonItem.getEndDate();
                respModels.add(model);
            }
        }

        return respModels;
    }

    public static class ReqModel extends YxyApiReqModel {

        public String userId;

        @Override
        public void autoCheck() {
            Assert.isTrue(StringUtils.isNotBlank(userId), "用户ID为空");
        }

    }

    public static class RespModel extends YxyApiRespModel {

        public Date bTime;

        public Date eTime;

    }

}
